<template>
  <div>
    <div class="header">
      <div class="search">
        <input type="text" name="" id="" placeholder="面保证金用电脑" />
      </div>
    </div>
    <div class="filter">
      <ul>
        <li @click="handleClick(0)" :class="num == 0 ? 'active' : ''">
          默认排行
        </li>
        <li @click="handleClick(3)" :class="num == 3 ? 'active' : ''">销量</li>
        <li class="li-3">
          价格
          <span class="icon">
            <van-icon
              @click="handleClick(2)"
              :class="num == 2 ? 'active' : ''"
              name="arrow-up"
              size="10"
            />
            <van-icon
              @click="handleClick(1)"
              :class="num == 1 ? 'active' : ''"
              name="arrow-down"
              size="10"
            />
          </span>
        </li>
        <li>筛选</li>
      </ul>
    </div>
    <div class="category" ref="wrapper">
      <ul>
        <li v-for="(item, index) in list" :key="index">
          <div class="img">
            <img :src="item.pic" alt="" />
          </div>
          <div class="tatal">
            <p>
              {{ item.title }}
            </p>
            <div class="bothmsg">
              <span>随用随还</span>
              <span>分期销售</span>
            </div>
            <span>￥{{ item.price }}</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import BScroll from "@better-scroll/core";
import Pullup from "@better-scroll/pull-up";
import axios from "axios";
import Vue from "vue";
import { Icon } from "vant";
Vue.use(Icon);
BScroll.use(Pullup);
import qs from "qs";
let p = 1;
export default {
  data() {
    return {
      list: [],
      sort: 0,
      index: 41,
      num: 0,
    };
  },
  created() {
    let { id } = this.$route.params;
    this.index = this.$route.params.id;
    console.log(id);
    let url = "https://m.edianyun.com/newsCategory/queryAllProductForNew";
    var data = qs.stringify({
      time: " 1631881359122",
      code: " 86b4f4962318b968b154ffe6188f909a",
      page: "1",
      size: " 10",
      errorCheck: " 1",
      categoryId: `${id}`,
      sort: "0",
      title: " ",
      searchStatus: " 1",
      plat: " 2",
      notFoundStatus: " 1",
      secondSearchStatus: " 1",
      customerId: "",
    });
    axios({
      url,
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: data,
    }).then((res) => {
      // console.log(res);
      this.list = res.data.data.data.items;
      // console.log(this.list);
    });
  },
  mounted() {
    let {index}=this
    this.$nextTick(() => {
      let bs = new BScroll(this.$refs.wrapper, {
        //初始化一个BetterScroll实例
        pullUpLoad: true, //开启触底事件监听
      });
      bs.on("pullingUp", () => {
        console.log("触底了");
        if (p <= 3) {
          p++;
          console.log(p);
          let url = "https://m.edianyun.com/newsCategory/queryAllProductForNew";
          var data = qs.stringify({
            time: " 1631881359122",
            code: " 86b4f4962318b968b154ffe6188f909a",
            page: `${p}`,
            size: " 10",
            errorCheck: " 1",
            categoryId: `${index}`,
            sort: "0",
            title: " ",
            searchStatus: " 1",
            plat: " 2",
            notFoundStatus: " 1",
            secondSearchStatus: " 1",
            customerId: "",
          });
          axios({
            url,
            method: "POST",
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            data: data,
          }).then((res) => {
            //   数组的结构赋值
            let [...Products] = res.data.data.data.items;
            // console.log(Products,res.data.data.data.items)
            this.list = [
              ...this.list, //原数据
              ...Products, //新数据
            ];
            bs.refresh(); //更新了DOM数量后，需要告知BetterScroll，重新计算内容高度
            bs.finishPullUp(); //保证下次触底依旧可以出发事件
          });
        }
      });
    });
  },
  methods: {
    handleClick(n) {
      this.sort = n;
      this.num = n;
      // console.log(this.index);
    },
  },
  computed: {
    filterlist() {
      return this.sort;
    },
  },
  watch: {
    filterlist(newValue) {
      console.log(newValue);
      let { sort, index } = this;
      let url = "https://m.edianyun.com/newsCategory/queryAllProductForNew";
      var data = qs.stringify({
        time: " 1631881359122",
        code: " 86b4f4962318b968b154ffe6188f909a",
        page: "1",
        size: " 10",
        errorCheck: " 1",
        categoryId: `${index}`,
        sort: `${sort}`,
        title: " ",
        searchStatus: " 1",
        plat: " 2",
        notFoundStatus: " 1",
        secondSearchStatus: " 1",
        customerId: "",
      });
      axios({
        url,
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        data: data,
      }).then((res) => {
        this.list = res.data.data.data.items;
        console.log(this.list);
        this.$nextTick(() => {
          let bs = new BScroll(this.$refs.wrapper, {
            //初始化一个BetterScroll实例
            pullUpLoad: true, //开启触底事件监听
          });
          bs.on("pullingUp", () => {
            console.log("触底了");
            if (p <= 3) {
              p++;
              console.log(p);
              let url =
                "https://m.edianyun.com/newsCategory/queryAllProductForNew";
              var data = qs.stringify({
                time: " 1631881359122",
                code: " 86b4f4962318b968b154ffe6188f909a",
                page: `${p}`,
                size: " 10",
                errorCheck: " 1",
                categoryId: `${index}`,
                sort: `${sort}`,
                title: " ",
                searchStatus: " 1",
                plat: " 2",
                notFoundStatus: " 1",
                secondSearchStatus: " 1",
                customerId: "",
              });
              axios({
                url,
                method: "POST",
                headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
                data: data,
              }).then((res) => {
                //   数组的结构赋值
                let [...Products] = res.data.data.data.items;
                // console.log(Products,res.data.data.data.items)
                this.list = [
                  ...this.list, //原数据
                  ...Products, //新数据
                ];
                bs.refresh(); //更新了DOM数量后，需要告知BetterScroll，重新计算内容高度
                bs.finishPullUp(); //保证下次触底依旧可以出发事件
              });
            }
          });
        });
      });
      return this.list;
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  position: fixed;
  height: 70px;
  z-index: 1000;
  top: 0;
  left: 0;
  border-bottom: 1px solid #f4f5f7;
  background: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  .search {
    width: 85%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    input {
      width: 100%;
      height: 32px;
      line-height: 25px;
      padding: 10px 10px;
      // line-height: .8rem;
      border: 1px solid transparent;
      box-sizing: border-box;
      border-radius: 12px;
      outline: none;
      background: #f1f1f1;
      -webkit-appearance: none;
    }
  }
}
.filter {
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  font-size: 14px;
  height: 50px;
  padding: 0 10px;
  margin-top: 71px;
  display: flex;
  justify-content: center;
  ul {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    line-height: 40px;
    .active {
      color: #1296db;
    }
  }
}
.category {
  width: 100%;
  overflow: hidden;
  height: 500px;
  ul {
    width: 100%;
    display: flex;
    flex-direction: column;
    background: #fff;
    li {
      border-bottom: 1px solid #e8e8e8;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;
      img {
        width: 110px;
        height: 110px;
      }
      .tatal {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        p {
          width: 243px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 3;
          word-break: break-all;
          text-overflow: ellipsis;
          overflow: hidden;
          font-size: 12px;
          color: #666d71;
        }
        .bothmsg {
          display: flex;
          margin: 10px 0;
          span {
            font-size: 12px;
            padding: 0 4px;
            border: 1px solid #ef334d;
            color: #ef334d;
            line-height: 20px;
            box-sizing: border-box;
            border-radius: 3px;
            margin-right: 3px;
          }
        }
        span {
          display: flex;
          font-size: 16px;
          color: #ef334d;
        }
      }
    }
  }
}
.filter .li-3 {
  display: flex;
}
.filter .icon {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
</style>